package de.lmu.mvs.lerngruppenfinder;

import java.io.IOException;
import java.util.ArrayList;

import com.eclipsesource.json.JsonArray;

import de.lmu.mvs.lerngruppenfinder.utils.BaseActivity;
import de.lmu.mvs.lerngruppenfinder.utils.FileApi;
import de.lmu.mvs.lerngruppenfinder.utils.LocationService;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector.ServerConnectorListener;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector.ServerError;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

public class MainActivity extends BaseActivity {
	private boolean[] preloaderSuccess;
	private final int prelaodAmaount=2;
	
	private static final String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		preloaderSuccess = new boolean[prelaodAmaount];
		for (int i=0;i<preloaderSuccess.length;i++){
			preloaderSuccess[i]=false;
		}
		
		setContentView(R.layout.activity_main);
		startService(new Intent(this, LocationService.class)); 
		preloadData();
	}

	public void startNextActivity(View view) {
		//Login Activity starten
		startActivity(new Intent(this, LoginActivity.class));
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == SetInterestActivity.REQUEST_INTEREST) {
	        if (resultCode == RESULT_OK) {
	        	if(data != null){
	        		Log.d(TAG, data.getStringExtra("interests"));
	        	}
	        }
	    }
	}
	
	/**
	 * Preload data and store in storage
	 * FIXME start login only if data has arrived or timeout.
	 * FIXME add timestamp to determine if we have to update
	 */
	private void preloadData(){
		ServerConnector.getInterests(new ServerConnectorListener<JsonArray>() {

			@Override
			public void requestFinished(JsonArray json) {
				try {
					FileApi.writeJsonToStorage(json, "interests", MainActivity.this);
					preloaderSuccess[0]=true;
					checkPreLoaders();
				} catch (IOException e) {
					e.printStackTrace();
					preloaderSuccess[0]=true;
					checkPreLoaders();
				}
			}

			@Override
			public void requestError(ServerError error) {
				Log.e(TAG, error.getErrorMessage());
			}
		});
		
		Location location = ((LocationManager) this.getSystemService(Context.LOCATION_SERVICE))
				.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
		double latitue = 48.1;
		double longitude = 11.1;
		if(location != null){
			latitue = location.getLatitude();
			longitude = location.getLongitude();
		}
		
		ServerConnector.getGroupsInRadius(new ServerConnectorListener<JsonArray>() {

			@Override
			public void requestFinished(JsonArray json) {
				try {
					FileApi.writeJsonToStorage(json, "groups", MainActivity.this);
					preloaderSuccess[1]=true;
					checkPreLoaders();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void requestError(ServerError error) {
				Log.e(TAG, error.getErrorMessage());
				preloaderSuccess[1]=true;
				checkPreLoaders();
			}
		}, latitue, longitude, 20000);
	}
	private void checkPreLoaders(){
		for (int i=0;i<preloaderSuccess.length;i++){
			if (preloaderSuccess[i]==false){
				return;
			}
		}
		startActivity(new Intent(this, LoginActivity.class));
		finish();
	}
}
